const { sequelize, DataTypes } = require('./config/database');
const { Activity, User } = require('./models');

async function testCreateActivity() {
  try {
    // 确保数据库连接正常
    await sequelize.authenticate();
    console.log('Database connection established successfully.');
    
    // 查找一个测试用户
    const user = await User.findOne();
    if (!user) {
      console.error('No users found in database. Please create a user first.');
      return;
    }
    
    console.log(`Using user: ${user.nickname} (ID: ${user.id})`);
    
    // 创建测试活动数据
    const activityData = {
      title: '测试活动',
      description: '这是一个测试活动，用于验证创建活动功能是否正常工作。',
      type: 'sports',
      mainImage: 'https://picsum.photos/400/300?random=1',
      startTime: new Date(Date.now() + 24 * 60 * 60 * 1000), // 明天
      endTime: new Date(Date.now() + 25 * 60 * 60 * 1000), // 明天+1小时
      location: {
        name: '测试地点',
        address: '测试地址',
        coordinates: [120.1551, 30.2741]
      },
      maxParticipants: 10,
      creatorId: user.id
    };
    
    console.log('Creating activity with data:', activityData);
    
    // 创建活动
    const activity = await Activity.create(activityData);
    console.log('Activity created successfully:', activity.id);
    
    // 查询创建的活动
    const createdActivity = await Activity.findByPk(activity.id);
    console.log('Retrieved activity:', createdActivity.title);
    
    console.log('Test completed successfully!');
    
  } catch (error) {
    console.error('Test failed:', error);
  } finally {
    await sequelize.close();
  }
}

testCreateActivity();